package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.TestItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TestCase;
import com.ruoyi.system.mapper.TestPlanMapper;
import com.ruoyi.system.domain.TestPlan;
import com.ruoyi.system.service.ITestPlanService;
import com.ruoyi.common.core.text.Convert;

/**
 * test_planService业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-07
 */
@Service
public class TestPlanServiceImpl implements ITestPlanService
{
    @Autowired
    private TestPlanMapper testPlanMapper;

    /**
     * 查询test_plan
     * 
     * @param planId test_plan主键
     * @return test_plan
     */
    @Override
    public TestPlan selectTestPlanByPlanId(Long planId)
    {
        return testPlanMapper.selectTestPlanByPlanId(planId);
    }

    /**
     * 查询test_plan列表
     * 
     * @param testPlan test_plan
     * @return test_plan
     */
    @Override
    public List<TestPlan> selectTestPlanList(TestPlan testPlan)
    {
//        return testPlanMapper.selectTestPlanList(testPlan);
        List<TestPlan> testPlanList = testPlanMapper.selectTestPlanList(testPlan);
        for (int i = 0; i < testPlanList.size(); i++) {
            Long testItemId = testPlanList.get(i).getItemId();
            TestItem testItem = testPlanMapper.selectTestItemByItemId(testItemId);
            testPlanList.get(i).setItemName(testItem.getItemName());
        }
        return testPlanList;
    }

    /**
     * 新增test_plan
     * 
     * @param testPlan test_plan
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTestPlan(TestPlan testPlan)
    {
        int rows = testPlanMapper.insertTestPlan(testPlan);
        insertTestCase(testPlan);
        return rows;
    }

    /**
     * 修改test_plan
     * 
     * @param testPlan test_plan
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTestPlan(TestPlan testPlan)
    {
        testPlanMapper.deleteTestCaseByPlanId(testPlan.getPlanId());
        insertTestCase(testPlan);
        return testPlanMapper.updateTestPlan(testPlan);
    }

    /**
     * 批量删除test_plan
     * 
     * @param planIds 需要删除的test_plan主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestPlanByPlanIds(String planIds)
    {
        testPlanMapper.deleteTestCaseByPlanIds(Convert.toStrArray(planIds));
        return testPlanMapper.deleteTestPlanByPlanIds(Convert.toStrArray(planIds));
    }

    /**
     * 删除test_plan信息
     * 
     * @param planId test_plan主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestPlanByPlanId(Long planId)
    {
        testPlanMapper.deleteTestCaseByPlanId(planId);
        return testPlanMapper.deleteTestPlanByPlanId(planId);
    }

    /**
     * 新增test_case信息
     * 
     * @param testPlan test_plan对象
     */
    public void insertTestCase(TestPlan testPlan)
    {
        List<TestCase> testCaseList = testPlan.getTestCaseList();
        Long planId = testPlan.getPlanId();
        if (StringUtils.isNotNull(testCaseList))
        {
            List<TestCase> list = new ArrayList<TestCase>();
            for (TestCase testCase : testCaseList)
            {
                testCase.setPlanId(planId);
                list.add(testCase);
            }
            if (list.size() > 0)
            {
                testPlanMapper.batchTestCase(list);
            }
        }
    }

    /**
     * 查询方法，这里可以到时候做数据验证，避免重复数据
     *
     */
    @Override
    public List<TestItem> selectTestItemName()
    {
        return testPlanMapper.selectTestItemName();
    }
}
